//
//  ScheduleManagerVC.m
//  MarryBackEnd
//
//  Created by KH_MoSi on 2020/8/1.
//  Copyright © 2020 WPP. All rights reserved.
//

#import "ScheduleManagerVC.h"
#import "ScheduleCalendarView.h"
#import "ScheduleShouldAvoidView.h"
#import "WPPPickerDayTimeView.h"
#import "ScheduleHistoryView.h"
//Model
#import <AFNetworking/AFNetworking.h>
#import "ScheduleCalendarServiceModel.h"
#import "ScheduleHistoryModel.h"
@interface ScheduleManagerVC ()
@property(nonatomic,strong)UIScrollView *mainView;
@property(nonatomic,strong)ScheduleCalendarView *calenderView;
@property(nonatomic,strong)ScheduleShouldAvoidView *shouldAvoidView;
@property(nonatomic,strong)ScheduleHistoryView *historyView;
@property(nonatomic,strong)NSString *dayTime;
//场地
@property(nonatomic,strong)NSString *packageId;
//进入控制器时间
@property(nonatomic,strong)NSString *inTime;
@end

@implementation ScheduleManagerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createProperty];
    [self createUI];
    self.inTime = [NSString stringWithFormat:@"%0.0f",[Utilities getCurrentTimestamp]];
}
- (void)dealloc
{
    [[StatisticManager shareManager]insertBrowseUserActiveQuantityWithToolType:@"3" WithInTime:self.inTime];
    NSLog(@"ScheduleManagerVC ---dealloc");
}
- (void)createProperty
{
    
}
- (void)createUI
{
    [self createNaviBarWithTitle:@"档期管理" backButtonBlock:nil];
    [self adjustNaviBarTitle];
    [self refreshUI];
}
- (void)refreshUI
{
    self.shouldAvoidView.top = self.calenderView.bottom + 12*ADAPTER_WIDTH;
    CGFloat orinY = self.shouldAvoidView.bottom + 12*ADAPTER_WIDTH;
    self.historyView.top = orinY;
    orinY = self.historyView.bottom + 20*ADAPTER_WIDTH + kBottomSafeHeight;
    self.mainView.contentSize = CGSizeMake(0, MAX(self.mainView.height + 1, orinY));
}
#pragma mark - click
- (void)clickDownButton
{
    NSLog(@"点击事件....");
    WK(weakSelf)
    WPPPickerDayTimeView *view = [[WPPPickerDayTimeView alloc]initWithFrame:CGRectZero];
    view.parentVC = self;
    view.scheduleLimit = YES;
    [view setBackTimesBlock:^(NSString * _Nonnull times) {
        NSLog(@"%@",times);
        [weakSelf.calenderView changeDateWithDayTime:times];
    }];
}
#pragma mark - 日历回调事件
- (void)createNetMethod
{
    WK(weakSelf)
    //历史
    {
        //session对象
        AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
        sessionManager.requestSerializer.timeoutInterval = 8.f;
        sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", @"text/json", @"text/plain", @"text/javascript", @"text/xml", @"image/*", nil];
        //参数
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        NSArray *subArray = [self.dayTime componentsSeparatedByString:@"-"];
        [parameters setValue:@"1.0" forKey:@"v"];
        [parameters setValue:subArray[1] forKey:@"month"];
        [parameters setValue:subArray[2] forKey:@"day"];
        [parameters setValue:@"d2c1e0f3379ba238608d872dc628b059" forKey:@"key"];
        //请求
        [sessionManager GET:@"http://api.juheapi.com/japi/toh" parameters:parameters headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            ScheduleHistoryModel *historyModel = [ScheduleHistoryModel modelWithJSON:responseObject];
            weakSelf.historyView.historyModel = historyModel;
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
    }
}
- (void)calendarViewCallBackWithDayModel:(ScheduleCalendarModel *)calendarModel
{
    _shouldAvoidView.calendarModel = calendarModel;
}
#pragma mark - lazy懒加载
- (UIScrollView *)mainView
{
    if (!_mainView) {
        UIScrollView *view = [[UIScrollView alloc]initWithFrame:CGRectMake(0, kTopHeight, WIDTH, HEIGHT - kTopHeight)];
        [self.view addSubview:view];
        view.backgroundColor = kBGDarkColor;
        if (@available(iOS 11.0, *))
        {
            view.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        else
        {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        _mainView = view;
    }
    return _mainView;
}
- (ScheduleCalendarView *)calenderView
{
    if (!_calenderView) {
        ScheduleCalendarView *view = [[ScheduleCalendarView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 375*ADAPTER_WIDTH)];
        [self.mainView addSubview:view];
        WK(weakSelf)
        [view setBackMonthBlock:^(NSString * _Nonnull month) {
            weakSelf.naviBarTitleLab.text = month;
        }];
        [view setBackDayTimeBlock:^(NSString * _Nonnull dayTime) {
            weakSelf.dayTime = dayTime;
            [weakSelf createNetMethod];
        }];
        [view setBackDayDataModelBlock:^(ScheduleCalendarModel * _Nonnull calendarModel) {
            [weakSelf calendarViewCallBackWithDayModel:calendarModel];
        }];
        [view begin];
        _calenderView = view;
    }
    return _calenderView;
}
- (ScheduleShouldAvoidView *)shouldAvoidView
{
    if (!_shouldAvoidView) {
        ScheduleShouldAvoidView *view = [[ScheduleShouldAvoidView alloc]initWithFrame:CGRectMake(14*ADAPTER_WIDTH, 0, WIDTH - 28*ADAPTER_WIDTH, 0)];
        [self.mainView addSubview:view];
        WK(weakSelf)
        [view setRefreshUIBlock:^{
            [weakSelf refreshUI];
        }];
        [view setCollectionBlock:^{
            [weakSelf.calenderView collectionBlockCallBack];
        }];
        _shouldAvoidView = view;
    }
    return _shouldAvoidView;
}
- (ScheduleHistoryView *)historyView
{
    if (!_historyView) {
        ScheduleHistoryView *view = [[ScheduleHistoryView alloc]initWithFrame:CGRectMake(14*ADAPTER_WIDTH, 0, WIDTH - 28*ADAPTER_WIDTH, 0)];
        [self.mainView addSubview:view];
        WK(weakSelf)
        [view setRefreshUIBlock:^{
            [weakSelf refreshUI];
        }];
        _historyView = view;
    }
    return _historyView;
}
- (void)adjustNaviBarTitle
{
    UIButton *downButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.naviBarView addSubview:downButton];
    [downButton setImage:[UIImage getPNGimageInBundleWithName:@"Arrow_down_18x14"] forState:UIControlStateNormal];
    downButton.frame = CGRectMake(0, kStatusBarHeight, 5 + 9 + 15, kNavBarHeight);//9x7
    downButton.imageEdgeInsets = UIEdgeInsetsMake((kNavBarHeight - 7*ADAPTER_WIDTH) * 0.5, 5, (kNavBarHeight - 7*ADAPTER_WIDTH) * 0.5, 15);
    
    self.naviBarTitleLab.font = kFontRegular(18);
    CGFloat titleWidth = [@"2020年120月" sizeWithFont:self.naviBarTitleLab.font].width;
    CGFloat oriX = (WIDTH - titleWidth - downButton.width) * 0.5;
    self.naviBarTitleLab.left = oriX + 10;
    self.naviBarTitleLab.width = titleWidth;
    self.naviBarTitleLab.height = kNavBarHeight;
    self.naviBarTitleLab.centerY = self.naviBarBackButton.centerY;
    downButton.left = self.naviBarTitleLab.right - 10;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickDownButton)];
    self.naviBarTitleLab.userInteractionEnabled = YES;
    [self.naviBarTitleLab addGestureRecognizer:tap];
    
    [downButton addTarget:self action:@selector(clickDownButton) forControlEvents:UIControlEventTouchUpInside];
}

@end
